package com.training.bt.controller;

import com.training.bt.DTO.PostDTO;
import com.training.bt.entity.Post;
import com.training.bt.entity.User;
import com.training.bt.mapper.UserMapper;
import com.training.bt.security.util.SecurityUtils;
import com.training.bt.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;

import java.util.List;

@RestController
@RequestMapping("/api/posts")
@RequiredArgsConstructor
public class PostController {

    @Autowired
    private final IPostService postService;
    private final UserMapper userMapper;

    @PostMapping
    public ResponseEntity<PostDTO> createPost(@RequestBody Post post) {
        // 从请求头获取用户ID
        User user = userMapper.selectById(SecurityUtils.getCurrentUser().getId());
        Long userId = user.getId();
        // Long userId = ( Long) request.getAttribute("userId");
        post.setUserId(userId);
        PostDTO dto = new PostDTO();
        dto.setUserId(user.getId());
        dto.setUsername(user.getUsername());
        dto.setPostTitle(post.getPostTitle());
        dto.setPostContent(post.getPostContent());
        dto.setPostTime(post.getPostTime().toString());

        postService.createPost(post);

        return ResponseEntity.ok(dto);
    }

    @GetMapping("/{postId}")
    public ResponseEntity<Post> getPostById(@PathVariable Long postId) {
        // return new ResponseEntity<>(postService.getPostById(postId), HttpStatus.OK);
        return new ResponseEntity<>(postService.getPostByIdWithCommentCount(postId), HttpStatus.OK);
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<Post>> getPostsByUserId(@PathVariable Long userId) {
        return new ResponseEntity<>(postService.getPostsByUserId(userId), HttpStatus.OK);
    }

    // @GetMapping
    // public ResponseEntity<List<Post>> getAllPosts() {
    // return new ResponseEntity<>(postService.getAllPosts(), HttpStatus.OK);
    // }

    @PutMapping
    public ResponseEntity<Post> updatePost(@RequestBody Post post) {
        return new ResponseEntity<>(postService.updatePost(post), HttpStatus.OK);
    }

    @DeleteMapping("/{postId}")
    public ResponseEntity<Void> deletePost(@PathVariable Long postId) {
        postService.deletePost(postId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping
    public ResponseEntity<List<Post>> getAllPostsWithCommentCount() {
        List<Post> posts = postService.getAllPostsWithCommentCount();
        return new ResponseEntity<>(posts, HttpStatus.OK);
    }

    // 根据帖子ID查询单个帖子及其评论数
    @GetMapping("/comments/{postId}")
    public ResponseEntity<Post> getPostByIdWithCommentCount(@PathVariable Long postId) {
        return new ResponseEntity<>(postService.getPostByIdWithCommentCount(postId), HttpStatus.OK);
    }

    // 根据用户ID查询该用户的所有帖子及其评论数
    @GetMapping("/user/{userId}/with-comments")
    public ResponseEntity<List<Post>> getPostsByUserIdWithCommentCount(@PathVariable Long userId) {
        return new ResponseEntity<>(postService.getPostsByUserIdWithCommentCount(userId), HttpStatus.OK);
    }
}